package vn.edu.tdt.it.domain;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;

public class CreateOntologyExample {

	/**
	 * @param args
	 */
	private static final String SOURCE = ":";
	private static final String URI_OWL = "http://it.tdt.edu.vn/entity";
	private static final String URI_REL = "http://it.tdt.edu.vn/relation";
	private static OntModel owlModel;
	private static OntModel testModel;

	public static void main(String[] args) {
		owlModel = ModelFactory.createOntologyModel();
		// Tao ra 4 class
		OntClass khtn = owlModel.createClass(URI_OWL + SOURCE
				+ "Khoa_hoc_tu_nhien");
		OntClass khxh = owlModel.createClass(URI_OWL + SOURCE
				+ "Khoa_hoc_xa_hoi");
		OntClass kt_vh = owlModel.createClass(URI_OWL + SOURCE
				+ "Ky_thuat_van_hoa");
		OntClass khtn_vt = owlModel.createClass(URI_OWL + SOURCE + "KHTN");
		OntClass khtn2 = owlModel.createClass(URI_OWL + SOURCE + "Khoa_hoc_tu_nhien");
		// Equivalent class
		khtn.addEquivalentClass(khtn_vt);
		// Tao lop con
		OntClass i_hoa = owlModel.createClass(URI_OWL + SOURCE + "Hoa_hoc");
		khtn.addSubClass(i_hoa);
		Property p_part_of = owlModel.createDatatypeProperty(URI_REL + SOURCE
				+ "part_of");
		i_hoa.addProperty(p_part_of, khtn);
		Individual iv_mon = owlModel.createIndividual(URI_OWL + SOURCE
				+ "Mon_hoa", i_hoa);
		//Property p = testModel.getProperty(URI_REL + SOURCE + "part_of");
		//OntProperty op = p.as(OntProperty.class);
		// ...
		// save the file
		try {
			FileWriter out = new FileWriter(new File("test.xml"));
			owlModel.write(out, "RDF/XML");
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// Test lai
		testModel = ModelFactory.createOntologyModel();
		Model m = testModel.read("file:///C:/Users/nbpla_000/MyProjects/LuanVan/test.xml");
		if (m == null) {
			System.out.println("Khong the mo file");
		}
		// Lat tat ca cac class
		List<OntClass> ls = testModel.listClasses().toList();
		System.out.println(ls.contains(URI_OWL + SOURCE
				+ "Ky_thuat_van_hoa"));
		for (OntClass l : ls) {
			System.out.println("URI = " + l.getURI() + "\t\t\t\tName = "
					+ l.getLocalName() + "\t\t" + l.listInstances().toList().size());
		}
		// Lay class KHTN
		// Nho them URI phia truoc
		OntClass test_khtn = testModel.createClass(URI_OWL + SOURCE
				+ "Khoa_hoc_tu_nhien2");
		if(test_khtn==null){
			System.out.println("E null kia!");
		}
		OntClass i = testModel.getOntClass(URI_OWL + SOURCE + "khoa_hoc_tu_nhien");
		if (i == null) {
			System.out.println("Khong tim thay");
		} else {
			System.out.println("Class: " + test_khtn.getURI());
			System.out.println("Invidual: " + i.getURI());
			//System.out.println("Relations: " + test_khtn.);
		}
		
		//System.out.println(op.getURI());
		// Add individual
		// Individual concept = owlModel.crea
		Graph g = testModel.getGraph();
		System.out.println(g.size());
		System.out.println(g.getClass().getName());
		System.out.println("Get relations ");
		List<Resource>list = testModel.listSubjectsWithProperty(p_part_of).toList();
		for (Resource item: list) { 
			System.out.println(item.getLocalName());
		}
		System.out.println(testModel.listStatements().toSet().size());
	}
}
